// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    pageTitle:"凉梓刷题",
    swiperImg:[],
    waittingFor:"*******",
    time_month:"请",
    time_day:"设",
    time_hour:"置",
    vip_ad:{
      title:"点击我扫码 开通VIP",
      Style:1,
      icon:"https://www.emojiall.com/en/header-svg/%F0%9F%92%8E.svg"
    },
    notice:"英语万能作文模板正式上线！抓紧背！能救命！！",/* 公告 */
    vipgzhTitle:"扫码关注公众号【凉梓刷题】",/* 弹窗最上方标题 */
    vipgzhCont:"根据公众号指引激活小程序",/* 弹窗文本 */
    vipShowImg:"https://copyright.bdstatic.com/vcg/creative/efbddd724e6fd504d3c1533bf7db11c2.jpg@h_1280",/* 弹窗图片 */
    vipShowFlag:0,/* 弹窗显示状态标识 */
    vipgzhbottom1:"长按识别二维码",/* 弹窗底部内容1 */
    vipgzhbottom2:"保存二维码，使用微信扫一扫",/* 弹窗底部内容2 */
    dati:"0",/* 今日答题数 */
    right:"0",/* 答对数 */
    rightLv:"0",/* 正确率 */
    tk_cont:{         /* 精选题库内容 */
      0:{cont1:"2048位同学正在学",cont2:"2028年真题",type:"1",id:"666"},
      1:{cont1:"2048位同学正在学",cont2:"2098年真题",type:"2",id:"667",money:"2.13"},
      2:{cont1:"2048位同学正在学",cont2:"2030年真题",type:"2",id:"667"},
    },//type:   1:Vip   2:价格   3:免费
    adminInfo:[],
    desiged:false,
    loginFlag:false,
    classContIntro:[],
    exList:[],
    class_info_img:'',
    user_exam_count:{
        sum:0,/* 答题总数 */
        correct:0,/* 答对总数 */
        accuracy:0 /* 正确率 */
    }
  },

  onLoad(){
    
  },
  /*  */
  onShow(){
    var that =this
    console.log(wx.getStorageSync('userInfo'));
    if(wx.getStorageSync('target')){
        // 获取目标日期字符串
        let targetDateString = wx.getStorageSync('target').date;
        // 转换为Date对象
        let targetDate = new Date(targetDateString);

        // 获取当前日期
        let currentDate = new Date();

        // 计算时间差（毫秒）
        let diff = targetDate - currentDate;

        // 检查是否已经过了目标日期
        if (diff < 0) {
            /* console.log('目标日期已过'); */
            wx.showToast({
              title: '已过目标日期',
            })
            return;
        }
        // 将时间差转换为月、天、小时
        // 假设一个月平均30天
        let daysInMonth = 30;
        let hoursInDay = 24;
        let msInHour = 1000 * 60 * 60;
        let months = Math.floor(diff / (msInHour * hoursInDay * daysInMonth));
        let days = Math.floor((diff % (msInHour * hoursInDay * daysInMonth)) / (msInHour * hoursInDay));
        let hours = Math.floor((diff % (msInHour * hoursInDay)) / msInHour);

        this.setData({
            waittingFor: wx.getStorageSync('target').title,
            time_month:months,
            time_day:days,
            time_hour:hours,
        })
    }
    if(!wx.getStorageSync('userInfo')){
        wx.showToast({
          title: '请先登录',
          icon:'error'
        })
        setTimeout(() => {
            wx.switchTab({
              url: '/pages/mine/mine',
            })
          }, 2000); // 延迟2秒后跳转
    }
    /* 获取轮播图 */
    this.getSwiperImage({})
    this.getCourseList()
    /* 公众号或微信获取 */
    this.getClassVip()
    /* 用户正确率 */
    this.getCurrentInfo()
  },
  /* 进入做题列表 */
  bindNavId(e){
    if(this.judgeVIP()==false){
        wx.showModal({
          title: '您还不是会员',
          content: '点击确定添加老师咨询咨询激活码',
          complete: (res) => {
            if (res.cancel) {
               return
            }
            if (res.confirm) {
                wx.previewImage({
                  urls: [this.data.class_info_img],
                })
            }
          }
        })
        return
    }
    console.log(e.currentTarget.dataset);
    var id = e.currentTarget.dataset.id
    if(e.currentTarget.dataset.num==0){
        wx.showToast({
          title: '暂无题库',
          icon:"none"
        })
        return
    }
    /* 判断是不是VIP */
    wx.navigateTo({
      url: '/pages/tkdetail/tkdetail?id='+id,
    })
  },
  /* 轮播图获取 */
  getSwiperImage(){
      wx.showLoading({
        title: '加载中',
      })
      wx.request({
        url: app.globalData.baseUrl + '/get_last_swiper',
        method:"GET",
        success:(res)=>{
            wx.hideLoading()
            console.log("轮播图获取",res);
            this.setData({
                swiperImg:res.data.url
            })
        },fail:(res)=>{
            wx.showToast({
              title: 'SERVER ERROR',
              icon:"none"
            })
        }
      })
  },
  /* 获得激活码 */
  getClassVip(){
    wx.showLoading({
        title: '加载中',
      })
      wx.request({
        url: app.globalData.baseUrl + '/get_last_classinfo',
        method:"GET",
        success:(res)=>{
            wx.hideLoading()
            console.log("公众号微信二维码",res);
            this.setData({
                class_info_img:res.data.url[0]
            })
        },fail:(res)=>{
            wx.showToast({
              title: 'SERVER ERROR',
              icon:"none"
            })
        }
      })
  },
  /* 获取科目列表 */
  getCourseList(){
      wx.showLoading({
        title: '初始化',
      })
      wx.request({
        url:app.globalData.baseUrl + '/api/course_question_count',
        method:"GET",
        success:(res)=>{
            wx.hideLoading()
            console.log(res);
            this.setData({
                exList:res.data.data
            })
        },fail:(res)=>{
            wx.hideLoading()
            console.log(res);
        }
      })
  },
  /* 获取我的答题和我的答对和我的正确率 */
  getCurrentInfo(){
      wx.request({
        url:app.globalData.baseUrl + '/get_user_stats',
        method:"POST",
        data:{
            userid:wx.getStorageSync('userInfo').id
        },success:(res)=>{
            wx.hideLoading()
            if(res.data.success==true){
                this.setData({
                    user_exam_count:{
                        sum:res.data.data.sum,/* 答题总数 */
                        correct:res.data.data.correct,/* 答对总数 */
                        accuracy:parseFloat(res.data.data.accuracy).toFixed(2) /* 正确率 */
                    }
                })
                console.log("正确率信息",this.data.user_exam_count);
            }
        },fail:(res)=>{
            wx.hideLoading()
            wx.showToast({
              title: 'serve error',
              icon:"none"
            })
        }
      })
  },
  bindSetDate(){
      wx.switchTab({
        url: '/pages/mine/mine',
      })
  },
  /* 刷题公告 */
  bindNavToGG(){
      app.globalData.webUrl = this.data.adminInfo.gonggaoUrl
      wx.navigateTo({
        url: '/pages/webView/webView',
      })
  },
  /* 轮播图公告 */
  swiperIndex(e){
      /* console.log(e.currentTarget.dataset.index); */
      app.globalData.webUrl =e.currentTarget.dataset.url
      wx.navigateTo({
        url: '/pages/webView/webView',
      })
  },
  /* 预览照片 */
  preViewImg(){
      wx.previewImage({
        urls: [this.data.adminInfo.adminImg],
      })
  },
  /* 更多学习记录 */
  moreStudyDounment(){
        if(!wx.getStorageSync('user').loginFlag || !wx.getStorageSync('user').loginFlag){
            wx.showToast({
              title: '请先登录',
              icon:'none'
            })
        }else{
            wx.navigateTo({
              url: '/pages/studyMore/studyMore',
            })
        }
  },
  exam(e){
        var index = e.currentTarget.dataset.index
        var id = this.data.classContIntro[index]._id
        var count =e.currentTarget.dataset.count
        if(this.judgeVIP()==false && this.data.classCont[index].vip==true){
            wx.showToast({
                title: '您还不是会员',
                icon:'none'

            })
            return
        }
        this.judgeBankNull(id,count)
  },
    judgeVIP(){
        /* console.log(wx.getStorageSync('user')); */
        console.log(wx.getStorageSync('userInfo'));
        if(wx.getStorageSync('userInfo').vip==0 || !wx.getStorageSync('userInfo')){
            return false
        }
    },
    onShareAppMessage() {
        return {
            title:"快去学习升本刷题",
            path: 'pages/index/index'
        }
    },
    onShareTimeline(){
        return{
            title:"快去学习升本刷题",
            query:"pages/index/index"
        }
    }
})
